﻿
<Assembly: WebResource("YCDotNetFramework.YCTextBoxAutoComplete.js", "application/x-javascript")> 

Namespace Controle

    Public Class YCTextBoxAutoComplete
        Inherits TextBox

        Private pHfSelectedValue As HtmlControls.HtmlInputHidden
        Private pSelectCommand As String
        Private pJsValidator As String = String.Empty

#Region "Public Property"

        Public Property Url() As String
            Get
                Return Me.Attributes("url")
            End Get
            Set(ByVal value As String)
                Me.Attributes.Add("url", ResolveUrl(value))
            End Set
        End Property

        Public WriteOnly Property FixPositionTop() As Integer
            Set(ByVal value As Integer)
                Me.Attributes.Add("TopFix", value)
            End Set
        End Property

        Public Property Value As String
            Get
                Return pHfSelectedValue.Value
            End Get
            Set(ByVal value As String)
                pHfSelectedValue.Value = value
            End Set
        End Property

        Public Property JsValidator As String
            Get
                Return pJsValidator
            End Get
            Set(ByVal value As String)
                pJsValidator = value
            End Set
        End Property

#End Region

        Public Sub Clear()
            pHfSelectedValue.Value = -1
            Me.Text = String.Empty
        End Sub

        Private Sub HiddenFieldValueChanged(ByVal sender As Object, ByVal e As EventArgs)
            pHfSelectedValue.Value = DirectCast(sender, HtmlControls.HtmlInputHidden).Value
        End Sub

        Protected Overrides Sub CreateChildControls()
            Me.Controls.Clear()
            MyBase.CreateChildControls()

            Dim hdnField As New HtmlControls.HtmlInputHidden
            hdnField.ID = Me.ID & "HfSelectedValue"
            AddHandler hdnField.ServerChange, AddressOf HiddenFieldValueChanged
            MyBase.Controls.Add(hdnField)
            pHfSelectedValue = hdnField
        End Sub

        Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
            MyBase.OnInit(e)
            MyBase.EnsureChildControls()
        End Sub

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            MyBase.Render(writer)
            pHfSelectedValue.RenderControl(writer)
        End Sub

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)

            Page.ClientScript.RegisterClientScriptResource(GetType(YCTextBoxAutoComplete), "YCDotNetFramework.YCTextBoxAutoComplete.js")
            With Me.Attributes
                .Add("onkeyup", String.Concat(JsValidator, "YCAutoComplete(this);"))
                .Add("autocomplete", "off")
                .Add("idHfSelectedValue", pHfSelectedValue.ClientID)
                .Add("onblur", "YCAutoCompleteFecha = setTimeout(function() { YCAutoCompleteRemoveAreaItens('" + Me.ClientID + "'); }, 3000);")
                .Add("txt", "")
            End With
        End Sub

    End Class

End Namespace
